//
//  @ Project : Galaxy Star Trading System
//  @ File Name : DataTime.h
//  @ Date : 2014/9/5
//  @ Author :
//
//


#if !defined(CDATETIME_H)
#define CDATETIME_H


namespace GSTrader
{
	namespace Util
	{
		using namespace std::chrono;

		typedef duration<double, std::ratio<1, 10000000> > Ticks; // 纳秒

		class TRADE_BASE_API CLTimeSpan
		{
		public:
			CLTimeSpan();
			virtual ~CLTimeSpan();

		private:
			int64 m_tSpan;
		};

		// 时间日期类
		class TRADE_BASE_API CTTime
		{
		public:
			CTTime();
			CTTime(time_t tTime);
			CTTime(const CTTime& timeSrc);
			CTTime(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec, int nDST = -1);

			virtual ~CTTime();

			const CTTime& operator = (const CTTime& timeSrc);
			const CTTime& operator = (const time_t& timeSrc);

			static time_t GetTimeBeginDay(time_t t);
			static CTTime GetCurTime();
			tm* GetLocalTm(tm* ptm) const;
			string Format(char* pFormat);

			time_t GetTime() const;
			int GetYear() const;
			int GetMonth() const;
			int GetDay() const;
			int GetHour() const;
			int GetMinute() const;
			int GetSecond() const;
			int GetDayOfWeek() const;

			bool operator == (CTTime time) const;
			bool operator != (CTTime time) const;
			bool operator < (CTTime time) const;
			bool operator > (CTTime time) const;
			bool operator <= (CTTime time) const;
			bool operator >= (CTTime time) const;
			time_t operator +(time_t time);
		private:
			time_t	m_time;
		};

		class TRADE_BASE_API CTimePoint
		{
		public:
			CTimePoint();
			CTimePoint(system_clock::time_point _time);
			CTimePoint(const time_t& _time);
			CTimePoint(const CTimePoint& _hptime);
			CTimePoint(int nYears, int nMonth, int nDay, int nHour, int nMin, int nSecond, int nIsDst = -1);
			Ticks Subtract(const CTimePoint& _hpTime);
			CTimePoint& operator=(const CTimePoint& _hpTime);
			CTimePoint& operator=(const time_t& _ttime);

			static CTimePoint Now();
			bool operator==(const CTimePoint& _hpTime);
			bool operator>(const CTimePoint& _hpTime);
			bool operator<(const CTimePoint& _hpTime);
			bool operator!=(const CTimePoint& _hpTime);
			bool operator>=(const CTimePoint& _hpTime);
			bool operator<=(const CTimePoint& _hpTime);


			int operator-(const CTimePoint& _hpTime);

			const system_clock::time_point& GetTime();
			void SetTime(time_t t, int nMilliSecond);
			virtual ~CTimePoint();

			static CTimePoint GetTimeBeginDay(CTimePoint& t);
			time_t GetTime_t() ;
			double GetTicks(Ticks& _ticks);

			string ToString(); // 默认的格式化
			string Format(string strFmt);
			void   ToString(string& TimeFormat, int prec = 0);//0 精确到秒 1 毫秒 2 微秒 3 纳秒

			void AddSecond(int nSecond);
			void AddMilliSecond(int nMilliSecond);

		private:
			system_clock::time_point m_time;
		};
	}
}

#endif // CDATETIME_H